package com.yfqy.app.modules.order.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.app.domain.vo.ResultVO;
import com.yfqy.app.modules.order.dto.OrderCreateRequest;
import com.yfqy.app.modules.order.dto.OrderQueryDTO;
import com.yfqy.app.modules.order.dto.RefundApplyRequest;
import com.yfqy.app.modules.order.service.OrderRefundService;
import com.yfqy.app.modules.order.service.OrderService;
import com.yfqy.app.modules.order.vo.OrderCreateResultVO;
import com.yfqy.app.modules.order.vo.OrderInfoVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户端的order控制器
 */
@RestController
@RequestMapping("/api/client/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderRefundService orderRefundService;


    /**
     * 确认订单、创建订单
     *
     * @param orderCreateRequest
     * @return
     */
    @PostMapping("/createOrder")
    public ResultVO<OrderCreateResultVO> createOrder(@RequestBody @Valid OrderCreateRequest orderCreateRequest) {
        return ResultVO.data(orderService.createOrder(orderCreateRequest));
    }

    /**
     * 用户订单分页列表
     *
     * @param orderQueryDTO
     * @return
     */
    @PostMapping("/getOrders")
    public ResultVO<PageInfo<OrderInfoVO>> getOrders(@RequestBody OrderQueryDTO orderQueryDTO) {
        return ResultVO.data(orderService.getOrders(orderQueryDTO));
    }

    /**
     * 用户订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResultVO<OrderInfoVO> detail(@PathVariable Long id) {
        return ResultVO.data(orderService.detail(id));
    }

    /**
     * 取消订单未支付的订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/cancelOrder/{orderId}")
    public ResultVO<Void> cancelOrder(@PathVariable Long orderId) {
        return orderService.cancel(orderId);
    }

    /**
     * 已支付且未出库的订单-申请退款
     *
     * @param orderId
     * @return
     */
    @PostMapping("/applyRefund/{orderId}")
    public ResultVO<Void> applyRefund(@PathVariable Long orderId) {
        return orderService.applyRefund(orderId);
    }

    /**
     * 已出库、已发货、已完成、部分退款
     * 申请售后
     *
     * @param afterSaleRequest
     * @return
     */
    @PostMapping("/applyAfterSales")
    public ResultVO<Void> applyAfterSales(@RequestBody @Valid RefundApplyRequest afterSaleRequest) {
        return orderRefundService.applyAfterSales(afterSaleRequest);
    }

    /**
     * 取消申请售后
     *
     * @param refundId
     * @return
     */
    @PostMapping("/cancelRefund/{refundId}")
    public ResultVO<Void> cancelRefund(@PathVariable Long refundId) {
        return orderRefundService.cancelRefund(refundId);
    }

}
